#!/bin/sh
set -e

if [ "$1" = "" ]; then
  echo "Error: Missing output folder path"
  echo "Usage: buildmacos /path/to/build/directory"
  exit 1
fi

echo "Creating translation binaries..."
if [ ! -d "$1/lang" ]; then
  mkdir "$1/lang"
fi
for file in *.ts; do
  basename=`basename "$file" .ts`
  lrelease "$file" -qm "$1/lang/$basename.qm"
done
echo "Done."
echo "Copying system translations..."
cp -v -r ./resources/lang/system $1/lang
echo "Done."

echo "Building resource binary..."
rcc -binary resource.qrc -o "$1/lmc.rcc"
echo "Done."

echo "Copying themes..."
cp -v -r ./resources/themes "$1"
rm -r -v "$1/themes/Classic"
echo "Done."

cp -v ./resources/text/license.txt "$1/COPYING"

echo "Copying sound files..."
cp -v -r ./resources/sounds "$1"
echo "Done."

echo "Copying plugins..."
if [ ! -d "$1/../Plugins/imageformats" ]; then
  if [ ! -d "$1/../Plugins" ]; then
    mkdir "$1/../Plugins"
  fi
  mkdir "$1/../Plugins/imageformats"
fi
cp -v $QTDIR/plugins/imageformats/libqgif.dylib "$1/../Plugins/imageformats/libqgif.dylib"
cp -v $QTDIR/plugins/imageformats/libqico.dylib "$1/../Plugins/imageformats/libqico.dylib"
cp -v $QTDIR/plugins/imageformats/libqjpeg.dylib "$1/../Plugins/imageformats/libqjpeg.dylib"
cp -v $QTDIR/plugins/imageformats/libqtiff.dylib "$1/../Plugins/imageformats/libqtiff.dylib"
echo "Done."

echo "Copying libraries..."
cp -v ../../lmcapp/lib/liblmcapp.2.dylib "$1/liblmcapp.2.dylib"
install_name_tool -change liblmcapp.2.dylib @executable_path/liblmcapp.2.dylib "$1/LAN-Messenger"
echo "Done."

exit 0
